#!/bin/bash

# 安装chrony服务
if ! command -v chronyd >/dev/null 2>&1; then
    if command -v yum >/dev/null 2>&1; then
        yum install -y chrony
    elif command -v apt-get >/dev/null 2>&1; then
        apt-get install -y chrony
    elif command -v dnf >/dev/null 2>&1; then
        dnf install -y chrony
    else
        echo "无法安装chrony服务"
        exit 1
    fi
fi

# 配置chrony.conf文件
cat > /etc/chrony.conf << EOF
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 3.pool.ntp.org
pool 0.pool.ntp.org
pool 1.pool.ntp.org
pool 2.pool.ntp.org
pool 3.pool.ntp.org
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
EOF

# 配置chronyd以chrony用户运行
if [ -f /etc/sysconfig/chronyd ]; then
    if grep -q "OPTIONS=" /etc/sysconfig/chronyd; then
        sed -i 's/OPTIONS=.*/OPTIONS="-u chrony"/' /etc/sysconfig/chronyd
    else
        echo 'OPTIONS="-u chrony"' >> /etc/sysconfig/chronyd
    fi
else
    echo 'OPTIONS="-u chrony"' > /etc/sysconfig/chronyd
fi

# 启用并启动chronyd服务
systemctl enable chronyd
systemctl start chronyd


echo "chrony服务配置完成"

